package com.heima.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AdChannelDto;

import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author 96558
 * @Date 2021/1/8 18:40
 * @Version 1.0
 **/
@Service

public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 根据名称分页查询频道列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(AdChannelDto dto) {
        //1.参数检测
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        dto.checkParam();

        //按照名称模糊查询
        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        //按照ord排序 升序
        lambdaQueryWrapper.orderByAsc(AdChannel::getOrd);
        IPage result = page(page, lambdaQueryWrapper);


        //3.结果封装
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;

    }

    @Override
    public ResponseResult insert(AdChannel channel) {
        //1.检查参数
        if (channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.保存
        channel.setCreatedTime(new Date());
        channel.setIsDefault(false);//非默认，有且只有一个频道默认
        save(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //修改

    @Override
    public ResponseResult update(AdChannel adchannel) {
       if (null==adchannel || adchannel.getId()==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
       }
       //先查后改
        if (adchannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //修改
        updateById(adchannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);

    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
       if (id==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
       }
        //2.判断当前频道是否存在 和 是否有效
        AdChannel adChannel = getById(id);
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //3.启用状态下不能删除
        if (adChannel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_CHANNEL_ISSTATUS_ERROR);
        }

        removeById(id);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }


}
